package com.baizhi.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.entity.PmsBrand;
import com.baizhi.dao.PmsBrandMapper;
import com.baizhi.service.IPmsBrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xhc
 * @since 2020-09-25
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements IPmsBrandService {

    @Resource
    private PmsBrandMapper pmsBrandMapper;

    @Override
    public Page<PmsBrand> selectAll(String name, Integer page, Integer size) {

        Page<PmsBrand> bzLogPage= new Page<>(page,size);

        QueryWrapper<PmsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        Page<PmsBrand> logPage =null;
        if (name!=null){
            logPage = pmsBrandMapper.selectPage(bzLogPage, queryWrapper);
        }else {
            logPage = pmsBrandMapper.selectPage(bzLogPage, null);
        }
        return logPage;
    }

    @Override
    public void insert(PmsBrand pmsBrand) {
        try {
            pmsBrandMapper.insert(pmsBrand);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public PmsBrand showOne(Integer id) {
        PmsBrand pmsBrand = pmsBrandMapper.selectById(id);
        return pmsBrand;
    }

    @Override
    public void delete(Integer id) {
        try {
            pmsBrandMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(PmsBrand pmsBrand) {
        try {
            pmsBrandMapper.updateById(pmsBrand);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteAll(List<PmsBrand> ids) {
        try {
            pmsBrandMapper.deleteBatchIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Workbook downLoadExcel() {
        List<PmsBrand> pmsBrands = pmsBrandMapper.selectList(null);
        for (PmsBrand pmsBrand : pmsBrands) {
            pmsBrand.setLogo("E:\\idea-space\\bzmalls\\bzmall-admin\\src\\main\\webapp\\img\\"+pmsBrand.getLogo());
        }
        ExportParams params = new ExportParams("所有品牌的数据","brands");
        Workbook workbook = ExcelExportUtil.exportExcel(params, PmsBrand.class, pmsBrands);
        return workbook;
    }





}
